var imageUtil = require('../../../utils/util.js');
//var showTip = require('/static/js/showTip.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httphost: app.data.httphost,
    httpserver: app.data.httpserver,
    tabs: ["商品", "详情", "评价"],
    actionSheetHidden: true,
    nullTip: true,
    // nodes: [{
    //   name: 'div',
    //   children: [{
    //     type: 'text',
    //     text: 'Hello World!'
    //   }]
    // }], 
    /**
     *  tabs: [
          { "tab_name": "商品", tab_href: "" },
          { "tab_name": "详情", tab_href: "" },
          { "tab_name": "评价", tab_href: "../comment/list/list" }
        ],
     */

    goods: {},
    goods_pic: [],
    couponlist: {},
    freight: {},
    th_td_list: {},
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    firstIndex: -1,
    attr_key_value_list: [],
    goods_count: 1,
    currentPage:1,
    score:"",
    scoreType:"",
  },
  /*
  头部
  */

  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var goods_id = options.goods_id;
    
    that.setData({
      score:options.score,
      scoreType:options.scoreType
    })
    wx.request({
      url: that.data.httpserver + 'scoreGoods/info',
      data: { goods_id: goods_id },
      header: wx.getStorageSync('header'),
      success: function (request) {
        console.log("---------//////-"+request)
        var screenHeight =0;
        wx.getSystemInfo({
          success: function (res) {
            screenHeight = res.windowHeight
          }
        })
        var goods = request.data.goods;
        console.log("=--=-=-=-=-=-=-=-"+goods.goods_pic);
        var goods_pic_arry = goods.goods_pic.split(',');
        var goods_detail = request.data.goods.goods_detail.replace(/src="/g, 'src="' + that.data.httphost).replace('<img', '<img mode="widthFix" style="width:100%"');
        var attr_list = request.data.attr_list;
        var comment_list = request.data.commentlist;
        var commentlist = new Array();
        for (var i = 0; i < comment_list.length; i++) {

          var username = comment_list[i].username;
          var addtime = comment_list[i].addtime;
          var comment_title = comment_list[i].comment_title;
          var comment_content = comment_list[i].comment_content;
          var comment_pic = comment_list[i].comment_pic;
          var head_img = comment_list[i].head_img;

          if (comment_pic != '') {

            var comment_pic_arry = [];
            for (var j = 0; j < comment_pic.split(',').length; j++) {
              comment_pic_arry.push(comment_pic.split(',')[j].replace('../', that.data.httphost + '../'));
            }
          }
          commentlist.push({ "username": username, "addtime": addtime, "comment_title": comment_title, "comment_content": comment_content, "comment_pic_arry": comment_pic_arry, "head_img": head_img });
        }
        that.setData({
          goods: goods,
          goods_detail: goods_detail,
          // goods_detail: [{
          //   name: 'div',
          //   children: [{
          //     type: 'text',
          //     text: goods_detail
          //   }]
          // }], 
          goods_score: goods.goods_score,
          goods_num: goods.goods_num,
          goods_pic: goods_pic_arry,
          pic: goods_pic_arry[0],
          attribute_detail_id: request.data.attribute_detail_id,
          commentlist: commentlist,
          page: request.data.page,
          totalPage: request.data.page.totalPage,
          currentPage: request.data.page.currentPage,
          collection: request.data.collection,
          couponlist: request.data.couponlist,
          freight: request.data.freight,
          attr_list: attr_list,
          cart_count: request.data.cart_count,
          includeGroup: attr_list,
          custom: request.data.custom,
          screenHeight: screenHeight
        })
        if (attr_list!=null){
        that.distachattr_value(attr_list);
        // 只有一个属性组合的时候默认选中  
        if (attr_list.length == 1) {
          for (var i = 0; i < attr_list[0].attr_key_value_list.length; i++) {
            that.data.attr_key_value_list[i].selected_value = attr_list[0].attr_key_value_list[i].attr_value;
          }
          that.setData({
            attr_key_value_list: that.data.attr_key_value_list
          });
        }
        }
      }
    })
  },
  /**
   * 优惠券
   */
  coupon() {
    wx.navigateTo({
      url: '../../coupon/list/list'
    })
  },
  /**
   * 购物车
  */
  cart_list() {
    wx.redirectTo({
      url: '../../cart/list'
    })
  },


  imageLoad: function (e) {
    var imageSize = imageUtil.imageUtil(e)
    this.setData({
      imagewidth: imageSize.imageWidth,
      imageheight: imageSize.imageHeight
    })
  },
//加入购物车/立即购买
  actionSheetTap: function (e) {
    // 调用API从本地缓存中获取数据
    var session_id = wx.getStorageSync('session_id');
    var header = wx.getStorageSync('header');
    if (session_id == '') {
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var submit_type = e.currentTarget.dataset.submit_type;
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden,
      submit_type: submit_type
    })
  },

  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  /* 获取数据 */
  distachattr_value: function (attr_list) {
    /** 
      将后台返回的数据组合成类似 
      { 
        attr_key:'型号', 
        attr_key_value_list:['1','2','3'] 
      } 
    */
    // 把数据对象的数据（视图使用），写到局部内  
    var attr_key_value_list = this.data.attr_key_value_list;
    // 遍历获取的数据  
    for (var i = 0; i < attr_list.length; i++) {
      for (var j = 0; j < attr_list[i].attr_key_value_list.length; j++) {
        var attrIndex = this.getAttrIndex(attr_list[i].attr_key_value_list[j].attr_key, attr_key_value_list);
        // console.log('属性索引', attrIndex);   
        // 如果还没有属性索引为-1，此时新增属性并设置属性值数组的第一个值；索引大于等于0，表示已存在的属性名的位置  
        if (attrIndex >= 0) {
          // 如果属性值数组中没有该值，push新值；否则不处理  
          if (!this.isValueExist(attr_list[i].attr_key_value_list[j].attr_value, attr_key_value_list[attrIndex].attr_values)) {
            attr_key_value_list[attrIndex].attr_values.push(attr_list[i].attr_key_value_list[j].attr_value);
          }
        } else {
          attr_key_value_list.push({
            attr_key: attr_list[i].attr_key_value_list[j].attr_key,
            attr_values: [attr_list[i].attr_key_value_list[j].attr_value]
          });
        }
      }
    }
    // console.log('result', attr_key_value_list)  
    for (var i = 0; i < attr_key_value_list.length; i++) {
      for (var j = 0; j < attr_key_value_list[i].attr_values.length; j++) {
        if (attr_key_value_list[i].attr_value_status) {
          attr_key_value_list[i].attr_value_status[j] = true;
        } else {
          attr_key_value_list[i].attr_value_status = [];
          attr_key_value_list[i].attr_value_status[j] = true;
        }
      }
    }
    this.setData({
      attr_key_value_list: attr_key_value_list
    });
  },
  getAttrIndex: function (attrName, attr_key_value_list) {
    // 判断数组中的attr_key是否有该属性值  
    for (var i = 0; i < attr_key_value_list.length; i++) {
      if (attrName == attr_key_value_list[i].attr_key) {
        break;
      }
    }
    // console.log("..." + i < attr_key_value_list.length ? i : -1);
    return i < attr_key_value_list.length ? i : -1;
  },
  isValueExist: function (value, valueArr) {
    // 判断是否已有属性值  
    for (var i = 0; i < valueArr.length; i++) {
      if (valueArr[i] == value) {
        break;
      }
    }
    return i < valueArr.length;
  },
  /* 选择属性值事件 */
  selectattr_value: function (e) {
    console.log(33333333333)
    /* 
    点选属性值，联动判断其他属性值是否可选 
    */
    var attr_key_value_list = this.data.attr_key_value_list;
    var index = e.currentTarget.dataset.index;//属性索引  
    var key = e.currentTarget.dataset.key;
    var value = e.currentTarget.dataset.value;
    if (e.currentTarget.dataset.status || index == this.data.firstIndex) {
      if (e.currentTarget.dataset.selected_value == e.currentTarget.dataset.value) {
        // 取消选中  
        this.disSelectValue(attr_key_value_list, index, key, value);
      } else {
        // 选中  
        this.selectValue(attr_key_value_list, index, key, value);
      }

    }
  },
  /* 选中 */
  selectValue: function (attr_key_value_list, index, key, value, unselectStatus) {
    // console.log('firstIndex', this.data.firstIndex);  
    var includeGroup = [];
    if (index == this.data.firstIndex && !unselectStatus) { // 如果是第一个选中的属性值，则该属性所有值可选  
      var attr_list = this.data.attr_list;
      // 其他选中的属性值全都置空  
      // console.log('其他选中的属性值全都置空', index, this.data.firstIndex, !unselectStatus);  
      for (var i = 0; i < attr_key_value_list.length; i++) {
        for (var j = 0; j < attr_key_value_list[i].attr_values.length; j++) {
          attr_key_value_list[i].selected_value = '';
        }
      }
    } else {
      var attr_list = this.data.includeGroup;
    }

    console.log('选中', attr_list, index, key, value);  
    for (var i = 0; i < attr_list.length; i++) {
      for (var j = 0; j < attr_list[i].attr_key_value_list.length; j++) {
        if (attr_list[i].attr_key_value_list[j].attr_key == key && attr_list[i].attr_key_value_list[j].attr_value == value) {
          includeGroup.push(attr_list[i]);
        }
      }
    }
    attr_key_value_list[index].selected_value = value;

    // 判断属性是否可选  
    for (var i = 0; i < attr_key_value_list.length; i++) {
      for (var j = 0; j < attr_key_value_list[i].attr_values.length; j++) {
        attr_key_value_list[i].attr_value_status[j] = false;
      }
    }
    for (var k = 0; k < attr_key_value_list.length; k++) {
      for (var i = 0; i < includeGroup.length; i++) {
        for (var j = 0; j < includeGroup[i].attr_key_value_list.length; j++) {
          if (attr_key_value_list[k].attr_key == includeGroup[i].attr_key_value_list[j].attr_key) {
            for (var m = 0; m < attr_key_value_list[k].attr_values.length; m++) {
              if (attr_key_value_list[k].attr_values[m] == includeGroup[i].attr_key_value_list[j].attr_value) {
                attr_key_value_list[k].attr_value_status[m] = true;
              }
            }
          }
        }
      }
    }
    this.setData({
      attr_key_value_list: attr_key_value_list,
      includeGroup: includeGroup
    });

    var count = 0;
    for (var i = 0; i < attr_key_value_list.length; i++) {
      for (var j = 0; j < attr_key_value_list[i].attr_values.length; j++) {
        if (attr_key_value_list[i].selected_value) {
          count++;
          break;
        }
      }
    }
    if (count < 2) {// 第一次选中，同属性的值都可选  
      this.setData({
        firstIndex: index
      });
    } else {
      this.setData({
        firstIndex: -1
      });
    }
    if (count == attr_key_value_list.length) {//如果选择数等于数组属性数，则选择完毕
      // var value=[];
      var reday_attr_arry = [];
      // var key_value = {};
      for (var i = 0; i < attr_key_value_list.length; i++) {
        // console.log(attr_key_value_list[i]);
        if (!this.data.attr_key_value_list[i].selected_value) {
          break;
        }
        reday_attr_arry.push(this.data.attr_key_value_list[i].selected_value);
        // console.log(value);
        // attr_key_value_list[i].attr_key_value_list = 
      }
      var goods_detail = [];
      for (var i = 0; i < attr_list.length; i++) {
        if (reday_attr_arry.toString() == attr_list[i].goods_attr.toString()) {
          goods_detail = attr_list[i];
          console.log("============"+goods_detail.attribute_detail_score)
          this.setData({
            goods_score: goods_detail.attribute_detail_score,
            goods_num: goods_detail.attribute_detail_num,
            attribute_detail_id: goods_detail.attribute_detail_id,
            goods_count: 1
          })
        }
      }
    } else {
      var goods = this.data.goods;
      console.log(goods)
      this.setData({
        goods_score: goods.goods_score,
        goods_num: goods.goods_num,
        attribute_detail_id: 0,
        goods_count: 1
      })
    }
  },
  /* 取消选中 */
  disSelectValue: function (attr_key_value_list, index, key, value) {
    var attr_list = this.data.attr_list;
    attr_key_value_list[index].selected_value = '';

    // 判断属性是否可选  
    for (var i = 0; i < attr_key_value_list.length; i++) {
      for (var j = 0; j < attr_key_value_list[i].attr_values.length; j++) {
        attr_key_value_list[i].attr_value_status[j] = true;
      }
    }
    this.setData({
      includeGroup: attr_list,
      attr_key_value_list: attr_key_value_list
    });

    for (var i = 0; i < attr_key_value_list.length; i++) {
      if (attr_key_value_list[i].selected_value) {
        this.selectValue(attr_key_value_list, i, attr_key_value_list[i].attr_key, attr_key_value_list[i].selected_value, true);
      }
    }
  },
  // 增加数量
  addCount(e) {
    var goods_count = this.data.goods_count;
    goods_count = parseInt(goods_count) + 1;
    this.setData({
      goods_count: goods_count
    });
  },
  // 减少数量
  minusCount(e) {
    var goods_count = this.data.goods_count;
    if (goods_count <= 1) {
      return false;
    }
    goods_count = goods_count - 1;
    this.setData({
      goods_count: goods_count
    });
  },
  //确定加入购物车
  add_cart() {
    console.log("加购确定------------")
    var that = this;
    var goods_id = that.data.goods.goods_id;
    var goods_count = that.data.goods_count;
    var attribute_detail_id = that.data.attribute_detail_id;
    var sys_num = that.data.goods_num;
    var total_score = goods_count*that.data.goods_score;
    if (attribute_detail_id == 0) {
      that.showTip('请选择商品规格属性');
      return;
    }
    if (goods_count > sys_num) {
      that.showTip('库存不足');
      return;
    }
    if (total_score > that.data.score) {
      that.showTip('您的积分不足');
      return;
    }
    wx.request({
      url: that.data.httpserver + 'cart/addScoreGoods',
      type: 'post',
      data: { goods_id: goods_id, goods_count: goods_count, attribute_detail_id: attribute_detail_id },
      header: wx.getStorageSync('header'),

      success: function (request) {
        that.setData({
          actionSheetHidden: true,
          cart_count: request.data.cart_count
        });
        if (request.data.result == 1) {
          // console.log(request.data.message);
          that.showTip(request.data.message);
        }
        else if (request.data.result == 1005) {
          // console.log(request.data.message);
          that.showTip(request.data.message);
        }
      }
    })
  },
  /**
   * 预支付界面
  */
  to_order() {
    var that = this;
    var goods_id = that.data.goods.goods_id;
    var goods_count = that.data.goods_count;
    var attribute_detail_id = that.data.attribute_detail_id;
    var sys_num = that.data.goods_num;
    var total_score = goods_count*that.data.goods_score;
    if (attribute_detail_id == 0) {
      that.showTip('请选择商品规格属性');
      return;
    }
    if (goods_count > sys_num) {
      that.showTip('库存不足');
      return;
    }
    if (total_score > that.data.score) {
      that.showTip('您的积分不足');
      return;
    }
    wx.navigateTo({
      url: '../../order/paymentscore/info?goods_id=' + goods_id + '&goods_count=' + goods_count+ '&attribute_detail_id=' + attribute_detail_id + '&address_id='+"&scoreType="+that.data.scoreType,
    })
  },
  collection() {
    console.log("sc11111111111111111")
    var that = this;
    var goods_id = that.data.goods.goods_id;
    wx.request({
      url: that.data.httpserver + 'collection/addScoreGoods',
      type: 'post',
      data: { goods_id: goods_id },
      header: wx.getStorageSync('header'),
      success: function (request) {
        //alert(data.result);
        if (request.data.result == 1) {
          that.setData({
            collection: 1,
          });
          wx.showToast({
            title: request.data.message,
            icon: 'success',
            duration: 2000
          })
        }
        else if (request.data.result == 1005) {
        }
      }
    })
  },
  collection_ed() {
    console.log("sc22222222222222222")
    var that = this;
    var goods_id = that.data.goods.goods_id;
    wx.request({
      url: that.data.httpserver + 'collection/delete',
      type: 'post',
      data: { goods_id: goods_id },
      header: wx.getStorageSync('header'),
      success: function (request) {
        //alert(data.result);
        if (request.data.result == 1) {
          that.setData({
            collection: null,
          });
          wx.showToast({
            title: request.data.message,
            icon: 'success',
            duration: 2000
          })
        }
        else if (request.data.result == 1005) {
        }
      }
    })
  },
  comment_list(){
    var that = this
    wx.showNavigationBarLoading();
    var goods_id = that.data.goods.goods_id;
    var currentPage = that.data.currentPage;
    wx.request({
      url: that.data.httpserver + 'app/comment/list',
      type: 'post',
      data: { goods_id: goods_id, currentPage: currentPage},
      header: wx.getStorageSync('header'),
      success: function (request) {
        wx.hideNavigationBarLoading() //完成停止加载
        var comment_list = request.data.list;
        var commentlist = that.data.commentlist;
        for (var i = 0; i < comment_list.length; i++) {

          var username = comment_list[i].username;
          var addtime = comment_list[i].addtime;
          var comment_title = comment_list[i].comment_title;
          var comment_content = comment_list[i].comment_content;
          var comment_pic = comment_list[i].comment_pic;
          var head_img = comment_list[i].head_img;
          // if (head_img!=''){
          //   if (head_img.indexOf('http') == -1) {
          //     head_img = that.data.httphost + head_img;
          //   }
          // }
         
          if (comment_pic != '') {
            var comment_pic_arry = [];
            for (var j = 0; j < comment_pic.split(',').length; j++) {
              comment_pic_arry.push(comment_pic.split(',')[j].replace('../', that.data.httphost + '../'));
            }
          }
          commentlist.push({ "username": username, "addtime": addtime, "comment_title": comment_title, "comment_content": comment_content, "comment_pic_arry": comment_pic_arry, "head_img": head_img });
        }
        that.setData({
          commentlist: commentlist
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  downLoad: function () {
    var totalPage = this.data.totalPage;
    var currentPage = this.data.currentPage;
    if (totalPage > currentPage) {
      currentPage++;
      this.setData({
        currentPage: currentPage,
      })
      this.comment_list();
    }
  },
  /**
   * 提示
   */
  showTip(tipTxt) {
    var that = this
    that.setData({
      nullTip : false, //弹窗显示
      tipTxt: tipTxt
    })
    setTimeout(function () {
      that.setData({
        nullTip : true, 
        tipTxt: tipTxt
      }) //1.5秒之后弹窗隐藏
    }, 1500)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})